// 节流
export const throttle = (func: () => {}, delay: number) => {
  let timer: NodeJS.Timeout
  return function () {
    let context = this
    let args = arguments
    if (!timer) {
      timer = setTimeout(function () {
        clearTimeout(timer)
        func.apply(context, args)
      }, delay)
    }
  }
}
// 防抖
export const debounce = (func: () => {}, delay: number) => {
  let timer: NodeJS.Timeout
  return function () {
    let context = this
    let args = arguments
    clearTimeout(timer)
    timer = setTimeout(function () {
      func.apply(context, args)
    }, delay)
  }
}


// 时间戳
export const formatTime = function (time: number) {//时间转换
  const timestamp = Date.now();
  return function () {
    const diff = timestamp - time  ;
    if (diff < 60 * 1000) {
      return '刚刚';
    } else if (diff < 60 * 60 * 1000) {
      return Math.floor(diff / (60 * 1000)) + '分钟前';
    } else if (diff < 24 * 60 * 60 * 1000) {
      return Math.floor(diff / (60 * 60 * 1000)) + '小时前';
    } else {
      const createTime = new Date(time * 1000);
      const Day = createTime.getDate();
      const Month = createTime.getMonth() + 1;
      const Hour = createTime.getHours();
      const Minute = createTime.getMinutes();

      function padding(str) {
        str = '' + str;
        if (str[1]) {
          return str;
        } else {
          return '0' + str;
        }
      }

      return `${padding(Month)}-${padding(Day)} ${padding(Hour)}:${padding(Minute)}`;
    }
  };
}
